package com.res.share.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.res.share.common.Constants;
import com.res.share.model.*;
import com.res.share.service.*;
import com.res.share.service.impl.ResReceiveServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

public class BaseController {

    @Autowired
    private ResCateGoryService resCateGoryService;

    @Autowired
    private IResUserService resUserService;

    @Autowired
    private IResTagService resTagService;

    @Autowired
    private IResLinkService resLinkService;

    @Autowired
    private CacheService cacheService;

    @Autowired
    private IResReceiveService receiveService;

    @Autowired
    private IResNoticeService resNoticeService;


    @Autowired
    private ThreadPoolExecutor executor;

    @ModelAttribute
    public void findBaseData(HttpServletRequest request, HttpServletResponse response, Model model) {
        //查询用户信息
        ResUser resUser = resUserService.findCurrentUserDetails(request, response);
        if (resUser != null) {
            model.addAttribute("loginFlag", true);
        } else {
            model.addAttribute("loginFlag", false);
        }
        model.addAttribute("resUser", resUser);

        CompletableFuture<Void> voidCompletableFuture1 = CompletableFuture.runAsync(() -> {
            List<ResCategory> resCategoryModelList = resCateGoryService.findResCategoryList(request, response);
            model.addAttribute("resCategoryModel", resCategoryModelList);
        }, executor);

        //查询标签
        CompletableFuture<Void> resTagsFuture = CompletableFuture.runAsync(() -> {
            List<ResTag> resTagList = resTagService.findTagsList();
            model.addAttribute("resTags", resTagList);
        }, executor);
        //查询友情链接
        CompletableFuture<Void> resLinkFuture = CompletableFuture.runAsync(() -> {
            List<ResLink> list = getResLinks();
            model.addAttribute("resLinkList", list);
        }, executor);

        //收件箱 未读数量
        //系统公告 未读数量
        CompletableFuture<Void> messageDataFuture = CompletableFuture.runAsync(() -> {
            if(resUser!=null){
                Integer readMsgNum=receiveService.findNoReadMessageNums(resUser.getId());
                Integer sysMsgNum=resNoticeService.findNoReadMessageNums(resUser.getId());
                model.addAttribute("readMsgNum", readMsgNum);
                model.addAttribute("sysMsgNum", sysMsgNum);
                model.addAttribute("msgTotal", readMsgNum+sysMsgNum);
            }
        }, executor);


        CompletableFuture.allOf(resLinkFuture,resTagsFuture,voidCompletableFuture1,messageDataFuture).join();
    }

    /**
     * 友情链接
     *
     * @return
     */
    private List<ResLink> getResLinks() {
        List<ResLink> list = new ArrayList<>();
        Object commonCache = cacheService.getCommonCache(Constants.LINK_BASE_INFO);
        if (commonCache != null && StringUtils.isNotEmpty(commonCache.toString())) {
            list = JSON.parseArray(commonCache.toString(), ResLink.class);
        } else {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("STATUS", 1);
            queryWrapper.eq("IS_DEL", 0);
            list = resLinkService.list();
            if (!CollectionUtils.isEmpty(list)) {
                cacheService.setCommonCache(Constants.LINK_BASE_INFO, JSON.toJSONString(list));
            }
        }
        return list;
    }

}
